package com.ourhealth.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ourhealth.constant.MessageConstant;
import com.ourhealth.constant.RedisMessageConstant;
import com.ourhealth.entity.Result;
import com.ourhealth.service.OrderService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("order")
public class OrderController {
    @Reference
    private OrderService orderService;
    @Autowired
    private JedisPool jedisPool;
    @RequestMapping("submit")
    public Result submit(@RequestBody Map map){
//        String name = (String) map.get("name");
//        int sex = (int) map.get("sex");
        String telephone = (String) map.get("telephone");
        String validateCode = (String) map.get("validateCode");
//        String orderDate = (String) map.get("orderDate");

        if (validateCode!=null && validateCode.trim()!=""){
            String code = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
            //判断验证码是否正确
            if (!validateCode.equals(code)){
                return new Result(false,"验证码输入错误");
            }else {
                Result order = orderService.order(map);
                return order;
            }
        }
        return new Result(false,"验证码不能为空");
    }

    @RequestMapping("findById")
    public Result findById(@Param("id")Integer id){
        Map order = orderService.findById(id);
        return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS,order);
    }

    @RequestMapping("validate")
    public Result validate(@Param("imgCode") String imgCode,@Param("deviceId") String deviceId){
        Jedis resource = jedisPool.getResource();
        String s = resource.get(deviceId);
        resource.close();
        if (s.equals(imgCode)){
            return new Result(true,"");
        }else {
            return new Result(false,"验证码错误");
        }
    }

}
